var ipaddress = "http://192.168.199.111:40123/servlet";
var pageCount = 10;
var timerloading;
var searchloading = null;

function getIpAddress(){
	loadata();
  	var hostport=document.location.host;
   	ipaddress="http://"+hostport+"/servlet";
} 

function loadata() {
	var element = document.getElementById("rotate");
	var r = 0;
	var i = 10;
	function rotate(){
		r += i;
		if( Math.abs(r) >= 360 ) {
			r = 0;
		}
		element.style.MozTransform = "rotate(" + r + "deg)";
		element.style.webkitTransform ="rotate(" + r + "deg)";
		element.style.msTransform = "rotate(" + r + "deg)";
		element.style.OTransform = "rotate(" + r + "deg)";
		element.style.transform = "rotate(" + r + "deg)";

		if(allapplist!=null && allapplist!="" && allapplist.length==4
		&&installAppList!=null&&installAppList!=""&&progressAppList!=null&&progressAppList!=""){
			clearInterval(timerloading);
			timerloading = null;
			var rotate = document.getElementById("startloading");
			rotate.style.display = "none";
			for(var j=0; j<4; j++){
				getInfoByType(j);
			}
			refreshScroll();
		}
	}
	timerloading = setInterval(rotate,60);
}

function searchLoad(){
	var element = document.getElementById("searchrotate");
	var rotate = document.getElementById("searchloading");
	rotate.style.display = "block";
	var searchlist = document.getElementById("searchlist");
	searchlist.style.display = "none";
	var r = 0;
	var i = 10;
	function rotateimg(){
		r += i;
		if( Math.abs(r) >= 360 ) {
			r = 0;
		}
		element.style.MozTransform = "rotate(" + r + "deg)";
		element.style.webkitTransform ="rotate(" + r + "deg)";
		element.style.msTransform = "rotate(" + r + "deg)";
		element.style.OTransform = "rotate(" + r + "deg)";
		element.style.transform = "rotate(" + r + "deg)";
	}
	searchloading = setInterval(rotateimg,50);
}

function finishSearchLoad(){
	clearInterval(searchloading);
	searchloading = null;
	var rotate = document.getElementById("searchloading");
	rotate.style.display = "none";
	var searchlist = document.getElementById("searchlist");
	searchlist.style.display = "block";
}
           			 
function getDataFromWeb() {
	getAppCategory();
	getInstallApps();
	getAllProgress();
}

function BackErr(er){
	alert("请确保设备网络连接正常,并和电视在同一局域网中");
}

function getAppCategory() {
	var url = ipaddress+"/superx/webdata/getAppCategory?ctype=class&_c=SkyMobileFans";
//	$.get(url,function(data){
//	json_to_appCategory(data);
//	})
	$.ajax({
	url: url,
	type: "GET",
	dataType:'json',
	success:function(data){
		json_to_appCategory(data);
	},
	error:function(er){
		BackErr(er);
	}
	});
}

function getAppList(i, classid, pageindex) {
	if(classid == -1){
		classid = appCategoryList[i].sortId;
	}
	var url = ipaddress+"/superx/webdata/getAppList?ctype=classlist&_c=SkyMobileFans&classid="+classid+"&pageindex="+pageindex+"&pagecount="+pageCount;
//	$.get(url,function(data){
//		json_to_applist(i, data);
//		listProcess[i] = false;
//	})
	$.ajax({
    	url: url,
    	type: "GET",
    	dataType:'json',
    	success:function(data){
    		json_to_applist(i, data);
			listProcess[i] = false;
    	},
    	error:function(er){
    		BackErr(er);
    		listProcess[i] = false;
    	}
    	});
}

function getInstallApps() {
	var url =ipaddress+"/superx/app/installedapps";
	$.get(url,function(data){
		json_to_installAppList(data);
		})
}

function getAllProgress() {
	var url =ipaddress+"/superx/app/getallprogress";
	$.get(url,function(data){
		json_to_getAllProgress(data);
		})
}

function startApp(pkgname,  clsname) {
	var url = ipaddress+"/superx/app/startapp?packageName="+pkgname+"&className="+clsname;
//	$.get(url,function(data){
//		})
	$.ajax({
			url: url,
			type: "GET",
			dataType:'json',
			timeout:5000,
			success:function(data){
		var dte = JSON.stringify(data);
			},
			error:function(er){
				BackErr(er);
			}
			});
}

function pushApp(fileName) {
	var url = ipaddress+"/superx/app/pushapk?fileName="+fileName;
//	$.get(url,function(data){
//		})
		$.ajax({
        	url: url,
        	type: "GET",
        	dataType:'json',
        	success:function(data){
        	},
        	error:function(er){
        		BackErr(er);
        	}
        	});
}

function getapkinfo(filename) {
	var url = ipaddress+"/superx/app/getapkinfo?filename="+filename;
	$.get(url,function(data){

		serverResultApkInfo = json_to_getApkInfo(data);
		})
}

function downloadApp(id, pkgname, title, img, tab, index) {
	var postUrl = encodeURIComponent(img);
	var titleUrl =  encodeURIComponent(title);
	var url = ipaddress+"/superx/app/download?id="+id+"&packageName="+pkgname+"&posterUrl=" + postUrl + "&title=" + titleUrl;
//	$.get(url,function(data){
//		})
		$.ajax({
			url: url,
			type: "GET",
			dataType:'json',
			timeout:5000,
			success:function(data){
				installAPKCallBack(tab,index)
			},
			error:function(er){
				BackErr(er);
			}
			});
}

function getProgress(pkgname, element) {
	var url = ipaddress+"/superx/app/getprogress?packageName="+pkgname;
	var process;
	var i = 0;
	$.get(url,function(data){
		process=json_to_progress(data);
		if(process == null) {
		return;
		}
		if(element != null){		
			element.className = "noborder";
			element.innerHTML = process;
		}
         if(process == "打开" ||process == "安装到电视") {    //安装成功
			if(element != null){
				element.className = "hasborder";
			}
             for(var i = 0; i < installingTab.length; i++) {
				var tabIndex = installingTab[i];
			  	var app = allapplist[tabIndex.tab];
			  	var packagename = app[tabIndex.index].packagename;
			  	if(packagename == pkgname) {
					installingTab.splice(i, 1);
					getInstallApps(); //更新已安装列表。
					break;
				}
             }
         }
	});
}

